package com.wondedDream.util;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class HtmlCrawler {

    public static String  parseMockExamJsonFile(){
        try {
            // 读取本地 HTML 文件
            File input = new File("E:\\pmp\\模考\\模考20221112_files\\模考20221112.html");
            Document doc = Jsoup.parse(input, "UTF-8");
            // 存储所有问题的列表
            List<Map<String, Object>> questions = new ArrayList<>();

            // 提取每个问题元素
            Elements questionItems = doc.select(".question-item");

            for (Element questionItem : questionItems) {
                Map<String, Object> questionData = new HashMap<>();

                // 提取题目
                Element questionElement = questionItem.selectFirst(".question-index + div p");
                if (questionElement!= null) {
                    questionData.put("question", questionElement.text());
                } else {
                    questionData.put("question", "");
                }

                // 存储选项的列表
                List<Map<String, String>> options = new ArrayList<>();
                // 提取选项元素
                Elements optionElements = questionItem.select(".question-select-item__option");
                for (Element optionElement : optionElements) {
                    Map<String, String> option = new HashMap<>();
                    Element optionLetter = optionElement.selectFirst("span");
                    Element optionContent = optionElement.selectFirst(".xe-preview__content p");
                    if (optionLetter!= null && optionContent!= null) {
                        option.put("letter", optionLetter.text());
                        option.put("content", optionContent.text());
                        options.add(option);
                    }
                }
                questionData.put("options", options);

                // 提取解析元素
                Element analysisElement = questionItem.selectFirst(".answer-analysis-text + div");
                if (analysisElement!= null) {
                    questionData.put("analysis", analysisElement.text());
                } else {
                    questionData.put("analysis", "");
                }

                // 提取正确答案
                Element correctAnswerElement = questionItem.selectFirst(".correct-answer-result");
                if (correctAnswerElement!= null) {
                    questionData.put("correctAnswer", correctAnswerElement.text());
                } else {
                    questionData.put("correctAnswer", "");
                }

                questions.add(questionData);
            }

            // 将结果转换为 JSON 格式
            ObjectMapper mapper = new ObjectMapper();
            try {
                String json = mapper.writeValueAsString(questions);
                return json;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String  parselxJsonFile(String localPath){
        try {
            File input = new File(localPath);
            Document document = Jsoup.parse(input, "UTF-8");
            Elements questionElements = document.select(".question-select");
            List<Map<String, Object>> questions = new ArrayList<>();

            for (Element questionElement : questionElements) {
                // 初始化存储问题信息的 Map
                Map<String, Object> questionMap = new HashMap<>();

                // 提取问题
                Element header = questionElement.select(".question-header").first();
                String question = header.select(".question-text").text() + " " + header.select(".question-index").text() + " " + header.select(".xe-preview__content").text();
                questionMap.put("question", question);

                // 提取答案选项
                Elements answerElements = questionElement.select(".question-select-item__option");
                List<Map<String, String>> answers = new ArrayList<>();
                for (Element answerElement : answerElements) {
                    Map<String, String> answer = new HashMap<>();
                    answer.put("letter", answerElement.select(".question-select-item__option span").first().text());
                    answer.put("content", answerElement.select(".image-text-box p").text());
                    answers.add(answer);
                }
                questionMap.put("options", answers);

                // 提取解析
                Element analysisElement1 = questionElement.select(".xe-preview__content").first();
                String analysis1 = analysisElement1.text();
                questionMap.put("analysis", analysis1);

                // 提取正确答案
                Element correctAnswerElement = questionElement.select(".correct-answer-result").first();
                String correctAnswer = correctAnswerElement.text();
                questionMap.put("correctAnswer", correctAnswer);

                questions.add(questionMap);
            }


            Gson gson = new Gson();
            String json = gson.toJson(questions);
            return json;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }


}